layui.define(['jquery'], function(exports){

    var $ = layui.$;

    // 模块名
    var MOD_NAME = 'btnRadio';

    // 外部接口
    var radio = {
        v: '1.0.0'
        ,index: layui[MOD_NAME] ? (layui[MOD_NAME].index + 10000) : 0
        ,config: {}
        // 在layui.event中注册事件
        ,on: function(events, callback){
            return layui.onevent.call(this, MOD_NAME, events, callback);
        }
        ,getInstance: function(id){ //获取当前实例对象
            var that = this;
            return this.that[id] || (that.that[id] = {});
        }
        ,getAllInstance: function(){ //获取所有实例对象
            return thisRadio.that;
        }
        // 将所有的实例与对应input的值进行同步
        ,syncAllChecked: function() {
            var that = this;
            var allInstance = this.getAllInstance();
            for(var index in allInstance) {
                var instance = allInstance[index];
                instance.syncChecked();
            }
        }
        // 添加/解除 禁用状态
        ,disabled: function(disabled, id) {
            if (id) {
                var that = this.getInstance(id);
                that.disabled(disabled);
            } else {
                var allInstance = this.getAllInstance();
                for(var index in allInstance) {
                    var instance = allInstance[index];
                    instance.disabled(disabled);
                }
            }
        }
    };

    //当前实例
    var thisRadio = function(){
        var that = this
            ,options = that.config
            ,id = options.id || that.index;
        thisRadio.that[id] = that; //记录当前实例对象
        return {
            reload: function(options){
                that.reload.call(that, options);
            },
            config: options
        }
    }

    //构造器
    var ButtonRadio = function(options){
        var that = this;
        that.index = ++radio.index;
        that.config = $.extend({}, that.config, radio.config, options);
        that.render();
    };

    //默认配置
    ButtonRadio.prototype.config = {
        elem: undefined,
        name: '',
        data: [],
        value: ''
    };

    //渲染
    ButtonRadio.prototype.render = function(){

        // var options = this.options;
        var config = this.config;

        this.elem = $(config.elem);
        config.name = config.name || config.elem.replace('#', '');
        if (!this.elem[0]) return;

        //解析数据
        var data = [];
        layui.each(config.data, function(index, item){
            if(typeof item === 'object'){
                data.push({
                    name: item.name,
                    value: item.value === undefined ? index : item.value
                });
            } else {
                data.push({
                    name: item,
                    value: item
                });
            }
        });

        //生成HTML
        var radioHTML = [];

        layui.each(data, function(index, item){
            var checked = (config.value === item.value) ? 'layui-btn-checked' : '';
            radioHTML.push('<button class="layui-btn '+ checked +'" value="'+ item.value +'">'+ item.name +'</button> ');
        });

        this.elem.html('<div class="layui-btn-group btn-radio">'+ radioHTML.join('') +'<div class="mask" /></div>');

        // 生成隐藏input
        // var input = $('<input type="hidden">');
        var input = $('<input style="display: none;">');
        input.attr('name', this.config.name);
        this.elem.append(input);
        this.input = input;

        //事件
        this.events();
    };

    //事件
    ButtonRadio.prototype.events = function(){
        var that = this;
        var config = this.config;

        //点击
        this.elem.find('.layui-btn').on('click', function(){
            var value = $(this).attr('value');
            // var text = $(this).text();
            config.value = value;

            //同步选中样式
            $(this).addClass('layui-btn-checked').siblings().removeClass('layui-btn-checked');

            // 执行配置的change回调
            config.change && config.change(value);

            // 获取过滤器
            var filter = that.elem.attr('lay-filter');

            // 执行layui.onevent中注册的事件
            layui.event.call(this, MOD_NAME, 'change('+ filter  +')', {
                elem: this,
                value: value,
                othis: that.elem
            });

            that.syncValue();
        });
    };

    //记录所有实例
    thisRadio.that = {}; //记录所有实例对象

    //获取当前实例对象
    thisRadio.getThis = function(id){
        var that = thisRadio.that[id];
        if(!that) hint.error('The ID option was not found in the radio instance');
        return that || null;
    }

    radio.reload = function(id, options){
        var that = thisRadio.getThis(id);
        that.reload(options);
        return thisRadio.call(that);
    }

    // 根据按钮同步input的值
    ButtonRadio.prototype.syncValue = function() {
        var value = this.elem.find('.layui-btn-checked').attr('value');
        this.config.value = value;
        this.input.val(value);
    };

    // 根据input的值同步按钮的选中状态
    ButtonRadio.prototype.syncChecked = function() {
        var value = this.input.val();
        this.config.value = value;
        if (value || value == '0') {
            this.elem.find('.layui-btn[value="'+ value +'"]').click();
        } else {
            this.elem.find('.layui-btn').removeClass('layui-btn-checked');
        }
    }

    // 禁用或者解除禁用
    ButtonRadio.prototype.disabled = function(disabled) {
        if (disabled) {
            this.elem.find('.btn-radio').addClass('btn-radio-disabled');
            this.elem.find('.mask').show();
        } else {
            this.elem.find('.btn-radio').removeClass('btn-radio-disabled');
            this.elem.find('.mask').hide();
        }
    }

    //获取值
    ButtonRadio.prototype.getValue = function(){
        return this.config.value;
    };

    //设置值
    ButtonRadio.prototype.setValue = function(value){
        this.elem.find('.layui-btn[value="'+ value +'"]').click();
    };

    //重载
    ButtonRadio.prototype.reload = function(options){
        $.extend(this.config, options);
        this.render();
    };

    //核心入口
    radio.render = function(options){
        var inst = new ButtonRadio(options);
        inst.config = $.extend({}, inst.config, options);

        return thisRadio.call(inst);
    };
    layui.link('../dist/layui/extend/btnRadio/btnRadio.css');

    exports(MOD_NAME, radio);
});
